const fs = require('fs');

const data = fs.readdirSync('./');
const images = data.filter(file => /\.png$/.test(file));
const previous = images.map(file => file.replace(/(@\dx)?\.png/, ''));
const uniq = [...new Set(previous)];
// const toCamelCase = uniq.map(file =>
//   file.replace(/(?:[\W_]+)(\w)/g, (match, $1) => $1.toUpperCase())
// );

// 生成index.ts内容
const imp = uniq.reduce((pre, cur) => {
  return pre + `const ${cur} = require('./${cur}.png');\n`;
}, '');
const exp = `\nexport default { ${uniq.join(', ')} };`;

// 生成index.d.ts内容
const typeDeclarations = `interface ImageAssets {
${uniq.map(name => `  ${name}: string;`).join('\n')}
}

declare const _default: ImageAssets;

export default _default;`;

// 写入文件
fs.writeFileSync('./index.ts', imp + exp);
fs.writeFileSync('./index.d.ts', typeDeclarations);

console.log('已生成 index.ts 和 index.d.ts 文件');
